package net.roseparadise.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.roseparadise.config.PropertiesConfig;
import net.roseparadise.model.Member;
import net.roseparadise.service.UserService;
import net.roseparadise.util.VOUtils;
import net.roseparadise.vo.UserVO;

import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/action/Login.htm")
@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "LoginController"), name = "LoginCheckAjax")   

//@SessionAttributes("currentUser")
public class LoginController {
	@Autowired
	private UserService userService;
	
	@RemoteMethod 
	public String checkManageRights(HttpSession session){
		String result = "false";
		UserVO uservo = (UserVO)session.getAttribute("currentUser");
		if(null==uservo){
			result = "notlogin";
		}else if(uservo.getRole() > (PropertiesConfig.getInstance().getInt("Rights.StudioMember")-1) ){
			result = "true";
		}		
		return result;
	}
	
	@RemoteMethod 
	public String checkNameUsed(String name){
		String result = "used";
		if(userService.checkMemberName(name)){
			result = "notused";
		}
		//System.out.println("checkNameUsed : "+name+" : "+result);
		return result;
	}
	
	@RequestMapping(params = "cmd=current")
	public void getCurrentUser(HttpServletRequest request,HttpServletResponse response){
		JSONObject json =new JSONObject();
		UserVO uservo = null;
		String result = "true";
		try {
			uservo = (UserVO)request.getSession().getAttribute("currentUser");
			if(null==uservo){
				result = "false";
			}
		} catch (Exception e) {
			result = "false";
			e.printStackTrace();
		}
		
		try {			
			json.put("success", result);
			json.put("currentUser", new JSONObject(uservo));
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "cmd=login")
	public void login(HttpServletRequest request,HttpServletResponse response){
		JSONObject json =new JSONObject();
		String result = "true";
		try {
			String name = VOUtils.validateParameter(request,"name","",2,30);
			String password = VOUtils.validateParameter(request,"password","",6,12);
			UserVO uservo = userService.MemberLogin(name, password);
			if(null!=uservo){
				request.getSession().setAttribute("currentUser", uservo);
			}else{
				result = "false";
			}
		} catch (Exception e) {
			result = "false";
			e.printStackTrace();
		}
		
		try {			
			json.put("success", result);
			//System.out.println(json.toString());
			response.getWriter().write(json.toString());//"{success : "+result+"}");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "cmd=logout")
	public void logout(HttpServletRequest request,HttpServletResponse response){		
		try {
			UserVO uservo = (UserVO)request.getSession().getAttribute("currentUser");
			if(null!=uservo){
				request.getSession().removeAttribute("currentUser");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "cmd=register")
	public String register(HttpServletRequest request,HttpServletResponse response){
		try {
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "register";
	}
	
	@RequestMapping(params = "cmd=adduser")
	public void addUser(HttpServletRequest request,HttpServletResponse response){
		JSONObject json =new JSONObject();
		String result = "true";
		try {
			
			String name = VOUtils.validateParameter(request,"name","",2,30);
			String password = VOUtils.validateParameter(request,"password","",6,12);
			String email = VOUtils.validateParameter(request,"email","",6,30);
			String telephone = VOUtils.validateParameter(request,"tel","",6,12);
			String qq = VOUtils.validateParameter(request,"qq","",5,15);
			String msn = VOUtils.validateParameter(request,"msn","",6,30);
			Member member = new Member(name,0,telephone,qq,msn,email, VOUtils.getCurrentDateString());
			UserVO user = userService.addMember(member, password);	
			if(null!=user){
				request.getSession().setAttribute("currentUser", user);
			}else{
				result = "false";
			}
		} catch (Exception e) {
			result = "false";
			e.printStackTrace();
		}
		
		try {
			json.put("success", result);
			response.getWriter().write(json.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@RequestMapping(params = "cmd=findpw")
	public String findPassword(HttpServletRequest request,HttpServletResponse response){
		try {
			System.out.println("cmd=findPassword");
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "register";
	}
}
